package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysRoleResourceRelateDAO;
import com.ourteam.system.dao.SysRoleResourceRelate;
import com.ourteam.system.dao.SysRoleResourceRelateDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:36
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysRoleResourceRelateDAOJdbcImpl extends AbstractSpringDAO
    implements ISysRoleResourceRelateDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, RelateCode, RelateName, Remarks, Status, SysResourceId,
            SysRoleId, SysRoleName, SysResourceTitleKey
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.RelateCode,
            _DataObjectProperty.RelateName, _DataObjectProperty.Remarks,
            _DataObjectProperty.Status, _DataObjectProperty.SysResourceId,
            _DataObjectProperty.SysRoleId, _DataObjectProperty.SysRoleName,
            _DataObjectProperty.SysResourceTitleKey
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysRoleResourceRelate, SysRoleResourceRelateDAOQueryBean> daoHelper;

    public AbstractSysRoleResourceRelateDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysRoleResourceRelate, SysRoleResourceRelateDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysRoleDAO.EntityName)
                                .append(" sysRole ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysRoleDAO.Id,
                    com.ourteam.system.dao.ISysRoleDAO.EntityName + ".",
                    "sysRole."));
            relatedJoinSQLBuffer.append(" = ").append(SysRoleId);

            relatedJoinSQLBuffer.append(" AND ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysRoleDAO.RoleType,
                    com.ourteam.system.dao.ISysRoleDAO.EntityName + ".",
                    "sysRole.")).append(" ").append("= 'ROLE' ");

            relatedJoinSQLBuffer.append(" AND ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysRoleDAO.Status,
                    com.ourteam.system.dao.ISysRoleDAO.EntityName + ".",
                    "sysRole.")).append(" ").append("= 'active' ");

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysResourceDAO.EntityName)
                                .append(" sysResource ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysResourceDAO.Id,
                    com.ourteam.system.dao.ISysResourceDAO.EntityName + ".",
                    "sysResource."));
            relatedJoinSQLBuffer.append(" = ").append(SysResourceId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysRoleResourceRelate
         * @throws Exception
         */
    public boolean checkIsUnique(SysRoleResourceRelate aSysRoleResourceRelate)
        throws Exception {
        if (aSysRoleResourceRelate == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysRoleResourceRelate.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysRoleResourceRelate
     * @throws Exception
     */
    public void insert(SysRoleResourceRelate aSysRoleResourceRelate)
        throws Exception {
        super.doInsert(aSysRoleResourceRelate);
    }

    /**
         * Batch Insert Datas
         * @param aSysRoleResourceRelate
         * @throws Exception
         */
    public void batchInsert(SysRoleResourceRelate[] aSysRoleResourceRelates)
        throws Exception {
        super.doBatchInsert(aSysRoleResourceRelates);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysRoleResourceRelateDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysRoleResourceRelate aSysRoleResourceRelate)
        throws Exception {
        delete(aSysRoleResourceRelate.getId());
    }

    /**
     * Update Data Selective
     * @param aSysRoleResourceRelate
     * @throws Exception
     */
    public void update(SysRoleResourceRelate aSysRoleResourceRelate)
        throws Exception {
        if (aSysRoleResourceRelate.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysRoleResourceRelate);

        SysRoleResourceRelate tempObj = findById(aSysRoleResourceRelate.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysRoleResourceRelate, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysRoleResourceRelates[]
     * @throws Exception
     */
    public void batchUpdate(SysRoleResourceRelate[] aSysRoleResourceRelates)
        throws Exception {
        super.doBatchUpdate(aSysRoleResourceRelates);
    }

    /**
         * Update Data By Condictions
         * @param aSysRoleResourceRelate
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysRoleResourceRelate aSysRoleResourceRelate,
        SysRoleResourceRelateDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysRoleResourceRelate, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysRoleResourceRelateDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysRoleResourceRelate aSysRoleResourceRelate)
        throws Exception {
        if (aSysRoleResourceRelate.getRelateCode() == null) {
            throw new Exception(
                "The SysRoleResourceRelate.relateCode value is not allow NULL");
        }

        if (aSysRoleResourceRelate.getRelateName() == null) {
            throw new Exception(
                "The SysRoleResourceRelate.relateName value is not allow NULL");
        }

        if (aSysRoleResourceRelate.getRemarks() == null) {
            throw new Exception(
                "The SysRoleResourceRelate.remarks value is not allow NULL");
        }

        if (aSysRoleResourceRelate.getStatus() == null) {
            throw new Exception(
                "The SysRoleResourceRelate.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysRoleResourceRelate findById(final long id)
        throws Exception {
        SysRoleResourceRelateDAOQueryBean queryBean = new SysRoleResourceRelateDAOQueryBean();

        SysRoleResourceRelateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysRoleResourceRelate[] SysRoleResourceRelates = query(queryBean);

        if (SysRoleResourceRelates.length == 0) {
            return null;
        } else {
            return SysRoleResourceRelates[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysRoleResourceRelate[]
         * @throws Exception
         */
    public SysRoleResourceRelate[] query(
        SysRoleResourceRelateDAOQueryBean queryBean) throws Exception {
        List result = super.doQuery(queryBean,
                new JdbcSysRoleResourceRelateRowMapper());

        return (SysRoleResourceRelate[]) result.toArray(new SysRoleResourceRelate[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysRoleResourceRelateDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysRoleResourceRelateDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysRoleResourceRelate[] aSysRoleResourceRelates,
        SysRoleResourceRelateDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysRoleResourceRelates, queryBean);
    }

    public void synchronize(SysRoleResourceRelate[] aSysRoleResourceRelates,
        SysRoleResourceRelateDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysRoleResourceRelate> callBack)
        throws Exception {
        daoHelper.synchronize(aSysRoleResourceRelates, queryBean, callBack);
    }

    public static class JdbcSysRoleResourceRelateRowMapper implements RowMapper<SysRoleResourceRelate> {
        public SysRoleResourceRelate mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysRoleResourceRelate object = new SysRoleResourceRelate();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object relateCode = null;

                relateCode = resultSet.getObject(columnIndex++);

                object.setRelateCodeNull(resultSet.getBoolean(columnIndex++));

                if (relateCode != null) {
                    setBeanValue(object, _DataObjectProperty.RelateCode,
                        relateCode);
                }

                Object relateName = null;

                relateName = resultSet.getObject(columnIndex++);

                object.setRelateNameNull(resultSet.getBoolean(columnIndex++));

                if (relateName != null) {
                    setBeanValue(object, _DataObjectProperty.RelateName,
                        relateName);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object sysResourceId = null;

                sysResourceId = resultSet.getObject(columnIndex++);

                object.setSysResourceIdNull(resultSet.getBoolean(columnIndex++));

                if (sysResourceId != null) {
                    setBeanValue(object, _DataObjectProperty.SysResourceId,
                        sysResourceId);
                }

                Object sysRoleId = null;

                sysRoleId = resultSet.getObject(columnIndex++);

                object.setSysRoleIdNull(resultSet.getBoolean(columnIndex++));

                if (sysRoleId != null) {
                    setBeanValue(object, _DataObjectProperty.SysRoleId,
                        sysRoleId);
                }

                Object sysRoleName = null;

                sysRoleName = resultSet.getObject(columnIndex++);

                object.setSysRoleNameNull(resultSet.getBoolean(columnIndex++));

                if (sysRoleName != null) {
                    setBeanValue(object, _DataObjectProperty.SysRoleName,
                        sysRoleName);
                }

                Object sysResourceTitleKey = null;

                sysResourceTitleKey = resultSet.getObject(columnIndex++);

                object.setSysResourceTitleKeyNull(resultSet.getBoolean(
                        columnIndex++));

                if (sysResourceTitleKey != null) {
                    setBeanValue(object,
                        _DataObjectProperty.SysResourceTitleKey,
                        sysResourceTitleKey);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
